package org.example.controller.tester;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.TokenStorage;
import org.example.entity.task.Move;
import org.example.service.impl.MoveServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/13
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/tester/move")
public class moveController {
    @Autowired
    private MoveServiceImpl moveService;

    @GetMapping("/list")
    public ApiResponse getMoveList(HttpSession session) {
        try {
            return ApiResponse.ok("获取成功",
                    moveService.getMoveList((Integer) session.getAttribute(TokenStorage.getToken())));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }

    @PostMapping("/create")
    public ApiResponse insertMove(HttpSession session, Move moveVO) {
        try {
            moveVO.setTester((Integer) session.getAttribute(TokenStorage.getToken()));
            if (moveService.insertMove(moveVO)) return ApiResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResponse.failure();
    }
}
